package siwi.map.android;

import com.wmw.Utility.Common;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.MarginLayoutParams;
import android.view.animation.Animation;
import android.view.animation.AnticipateInterpolator;
import android.view.animation.OvershootInterpolator;
import android.view.animation.RotateAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageButton;

public class MyAnimations{
	public static final int DurationMS = 300;
	
	private static AnimationsTogetherDone _animTogetherDone;
	private static int animDoneNum = 0;
	private static int	xOffset		= 15;
	private static int	yOffset		= -13;

	public static void initOffset(Context context){//由布局文件
		xOffset		= (int) (10.667 *context.getResources().getDisplayMetrics().density);
		yOffset		= -(int) (8.667 *context.getResources().getDisplayMetrics().density);
	}
	
	public static void setAnimDoneObj(AnimationsTogetherDone animTogetherDone){
		_animTogetherDone = animTogetherDone;
	}
	
	public static Animation getRotateAnimation(float fromDegrees ,float toDegrees,int durationMillis){
		RotateAnimation rotate = new RotateAnimation(fromDegrees, toDegrees,
				Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
		rotate.setDuration(durationMillis);
		rotate.setFillAfter(true);
		return rotate;
	}
	
	public static void StartAnimationsSpread(ViewGroup viewgroup,int durationMillis, Point centerPos) {
		for (int i = 0; i < viewgroup.getChildCount(); i++) {
				ImageButton inoutimagebutton = (ImageButton) viewgroup.getChildAt(i);
				inoutimagebutton.setVisibility(View.VISIBLE);
				MarginLayoutParams mlp = (MarginLayoutParams) inoutimagebutton.getLayoutParams();
				Animation animation = new TranslateAnimation(centerPos._x - mlp.leftMargin, 0F, centerPos._y - mlp.topMargin, 0F);
				
				animation.setFillAfter(true);animation.setDuration(durationMillis);
				animation.setStartOffset(((viewgroup.getChildCount()-i) * 100) / (-1 + viewgroup.getChildCount()));
				animation.setInterpolator(new OvershootInterpolator(2F));
				inoutimagebutton.startAnimation(animation);
		}
	}
	
	public static void StartAnimationsTogether(ViewGroup viewgroup,int durationMillis, Point centerPos, final boolean notifyAfterDone) {
		animDoneNum = 0;
		final int animTotalNum = viewgroup.getChildCount();
		for (int i = 0; i < animTotalNum; i++) {
				final ImageButton ib = (ImageButton) viewgroup.getChildAt(i);
				MarginLayoutParams mlp = (MarginLayoutParams) ib.getLayoutParams();
				Log.d(Common.GetLineInfo(), "left margin -" + mlp.leftMargin + ", right margin -" + mlp.rightMargin + ", top margin -" + mlp.topMargin + ", bottom margin -" + mlp.bottomMargin);
				Animation animation = new TranslateAnimation(0,centerPos._x - mlp.leftMargin, 0,centerPos._y - mlp.topMargin);
				
				animation.setFillAfter(true);animation.setDuration(durationMillis);
				animation.setStartOffset((i * 100) / (-1 + viewgroup.getChildCount()));//顺序倒一下比较舒服
				animation.setInterpolator(new AnticipateInterpolator(2F));
				
				animation.setAnimationListener(new Animation.AnimationListener() {
					@Override
					public void onAnimationStart(Animation arg0) {}
					@Override
					public void onAnimationRepeat(Animation arg0) {}
					@Override
					public void onAnimationEnd(Animation arg0) {
						ib.clearAnimation(); // Otherwise, setVisibility won't work.
						ib.setVisibility(View.INVISIBLE);
						++animDoneNum;
						if (notifyAfterDone && animDoneNum >= animTotalNum && _animTogetherDone != null)
							_animTogetherDone.onAnimationDone();
					}
				});
				ib.startAnimation(animation);
			}
	}
	
	public static class Point{
		public Point(int x, int y){
			_x = x;
			_y = y;
		}
		
		public int _x;
		public int _y;
	}

	public static void SetControlsPosition(Point []points, Point center, int radius, int numOfSplices, int startDegree, int endDegree, boolean includeStartAndEndDegree){
		float start = (float) (startDegree*Math.PI/180);
		float end = (float) (endDegree*Math.PI/180);
		numOfSplices = includeStartAndEndDegree ?  numOfSplices - 2 : numOfSplices;
		float theta = (float) ((end - start)/(numOfSplices + 1)); // not include startDegree and endDegree.
		
		int len = points.length;
		float delta = includeStartAndEndDegree ? start - theta : start;
		for (int i = 0; i < len; ++i)
		{
			delta += theta;
			points[i]._x = (int) (center._x + radius*Math.cos(delta));
			points[i]._y = (int) (center._y + radius*Math.sin(delta));
			Log.d(Common.GetLineInfo(), "x -" + points[i]._x + ", y -" + points[i]._y);
		}
		
	}
	
	public static interface AnimationsTogetherDone{
		public void onAnimationDone();
	}
	
	public static interface AnimationsSpreadDone{
		public void onAnimationDone();
	}
	
}